package com.mrper.ext.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;

import com.miguo.ui.R;
import com.miguo.ui.databinding.DialogShareActionsheetBinding;
import com.miguo.ui.databinding.GriditemShareActionsheetBinding;
import com.mrper.ext.adapter.TempleteAdapter;
import com.mrper.ext.annotation.ContentView;
import com.mrper.ext.uibase.BasicDialogFragment;
import com.mrper.model.share.ShareContentWithImageInfoEntry;
import com.mrper.util.DeviceUtil;
import com.mrper.util.ToastUtil;
import com.mrper.util.share.ShareUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by admin on 2016/3/14.
 * 分享ActionSheetDialog
 */
@ContentView(R.layout.dialog_share_actionsheet)
public class ShareActionSheetDialog extends BasicDialogFragment<DialogShareActionsheetBinding> implements AdapterView.OnItemClickListener{

    public static final String TAG = ShareActionSheetDialog.class.getSimpleName();

    public static final int[] SHARE_TYPE_ICON = {
            R.drawable.ic_share2wechatfriend,
            R.drawable.ic_share2wechat,
            R.drawable.ic_share2qzone,
//            R.drawable.ic_share2sinaweibo,
            R.drawable.ic_share2qqfriend
    };

    public static final int[] SHARE_TYPE_TEXT = {
            R.string.dialog_shareActionSheet_share2Wechat,
            R.string.dialog_shareActionSheet_share2WechatFriends,
            R.string.dialog_shareActionSheet_share2QZone,
//            R.string.dialog_shareActionSheet_share2SinaWeibo,
            R.string.dialog_shareActionSheet_share2QQ
    };

    private ShareContentWithImageInfoEntry shareContent;//分享的数据实体类
    private ShareUtil shareUtil;//分享对象

    @Override
    protected void initViews() {
        List<HashMap<String,Object>> shareTypeMap = new ArrayList<>();
        //添加分享项
        for(int i=0;i<SHARE_TYPE_ICON.length;i++){
            HashMap<String,Object> item = new HashMap<>();
            item.put("icon",SHARE_TYPE_ICON[i]);
            item.put("text",SHARE_TYPE_TEXT[i]);
            shareTypeMap.add(item);
        }
        dataBinder.gvShare.setAdapter(new TempleteAdapter<>(activity, R.layout.griditem_share_actionsheet, shareTypeMap,
                new TempleteAdapter.OnInitAdapterListener<GriditemShareActionsheetBinding, HashMap<String, Object>>() {
            @Override
            public void initSetValue(GriditemShareActionsheetBinding binder, HashMap<String, Object> item, int position) {
                binder.txtShareTypeText.setText(getString(Integer.valueOf(item.get("text").toString())));
                binder.imgShareTypeIcon.setImageResource(Integer.valueOf(item.get("icon").toString()));
            }
        }));
        dataBinder.gvShare.setOnItemClickListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        dataBinder.setOnClickEvent(this);//添加点击事件

        //获取要分享的数据
        Bundle argument = getArguments();
        if(argument!=null && argument.containsKey("shareContent"))
            this.shareContent = (ShareContentWithImageInfoEntry)argument.getSerializable("shareContent");

        //设置允许取消操作
        setCancelable(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(shareContent!=null) {
            switch (position) {
                case 0://分享到微信好友
                    shareUtil.startShare(ShareUtil.SHARE_TYPE_WECHAT,shareContent,2);//分享到微信平台 scence: 1.朋友圈 2.微信好友
                    break;
                case 1://分享到微信
                    shareUtil.startShare(ShareUtil.SHARE_TYPE_WECHAT,shareContent,1);//分享到微信平台 scence: 1.朋友圈 2.微信好友
                    break;
                case 2://分享到QQ空间
                    shareUtil.startShare(ShareUtil.SHARE_TYPE_QQ,shareContent,2);//分享到QQ平台 scence: 1.QQ好友 2.QQ空间
                    break;
//            case 3://分享到新浪微博
//                break;
                case 3://分享到QQ好友
                    shareUtil.startShare(ShareUtil.SHARE_TYPE_QQ,shareContent,1);//分享到QQ平台 scence: 1.QQ好友 2.QQ空间
                    break;
            }
        }else{
            ToastUtil.showShortToast(activity,R.string.dialog_shareActionSheet_shareContentIsNotCompleted);
        }
        this.dismiss();
    }

    public void onClick(View view){
        if(view.getId() == R.id.txtShareCancel)
            this.dismiss();
    }

    /** 设置分享回调  **/
    public void setShareUtil(ShareUtil shareUtil){
        this.shareUtil = shareUtil;
    }

    @Override
    protected int getDialogWidth() {
        return DeviceUtil.getScreenWidth(activity);
    }

    @Override
    protected int getDialogGravity() {
        return Gravity.BOTTOM;
    }
}
